<?php

// Namespace

namespace Loop;


/**
 * 
 */

class CViewFactory
{
	/**
	 * Generate the application sitemap
	 */

	public function api_generateSitemap()
	{
		// Call the method

		$this->generateSitemap();
	}


	/**
	 * Generate the application sitemap
	 */

	public function generateSitemap()
	{	
		// Dependencies

		require_once(PATH_LOOP . 'Component/Sitemap/Sitemap.php');


		// Get the extension ids

		$extensionIds = app()->getExtensionManager()->getExtensionsIds();

		//

		$sitemap = new \Sitemap(app()->getPathHelper()->getRootUrl(false)); 


		// Construct the path to xml files


		$pathXml = path 
		(
			array 
			(
				PATH_LOOP,
				'Xml'
			),
			true,
			false
		);

		$sitemap->setPath($pathXml);
		$sitemap->setFilename('sitemap');


		// Get the key of the path application

		$key = array_search(EXTENSION_APPLICATION, $extensionIds);


		// Unset the entry

		unset($extensionIds[$key]);


		// Set it first

		array_unshift($extensionIds, EXTENSION_APPLICATION);


		// For each extension ids

		foreach ($extensionIds as $extensionId)
		{
			// Define variable

			$pagesForSitemap = array();


			// Get the path to the extension

			$pathToExtension = app()->getPathHelper()->convertExtensionIdToPath($extensionId);


			// Define the path to the html files

			$pathHtmlFiles = path 
			(
				array 
				(
					$pathToExtension,
					'Html'
				),
				false,
				false
			);	


			// Does the directory exis

			if (is_dir($pathHtmlFiles) === true) 
			{
				// Get the directory's files

				$filesInDirectory= scandir($pathHtmlFiles);


				// For each file found


				foreach ($filesInDirectory as $file)
				{
					// Is it a directory ?

					if (is_dir($file) || $file == '.' || $file == '..')
					{
						continue;
					}


					// Get the extension

					$extension = '.' . pathinfo($pathHtmlFiles. $file , PATHINFO_EXTENSION);				


					// Is the extention allowed ?

					if (array_search($extension, array('.html', '.php')) === false)
					{
						// The extension is not allowed

						continue;
					}	


					// Is it a page ?

					preg_match_all('/^Page./', $file, $matchesPage);

					if (count($matchesPage[0]) > 0)
					{
						$pagesForSitemap[str_replace($extension, '', $file)] =  str_replace($extension, '', $file);
					}
				}


				// Sort natural

				natsort($pagesForSitemap);


				// For each page

				foreach ($pagesForSitemap as $page)
				{
					$languages = pref('[Loop]/localization/languages');

					foreach ($languages as $languageIsoCode)
					{
						if ($extensionId !== PATH_APPLICATION)
						{
							// Construct the uri

							$uri = '/' . $languageIsoCode . '/' .str_replace('.', '/', strtolower($page)) . '/';
						}
						else
						{
							// Construct the uri

							$uri ='/[' . $extensionId . ']/' . $languageIsoCode . '/' .str_replace('.', '/', strtolower($page)) . '/';
						}


						// Is the page accessible ?

						$view = app()->getViewManager()->createViewFromUri($uri);
						

						// Initialize the security manager

						app()->getSecurityManager()->initialize();


						// Get authorizations

						$isAuthorized = app()->getSecurityManager()->isPermissionOnViewByUserGroupGranted($view, ACTION_RENDER, '0');
					

						// Is it authorized ?

						if ($isAuthorized == true)
						{
							// Add it to sitemap

							$sitemap->addItem(substr($uri, 1));
						}
					}
				}
			}
		}


		// Close the file

		$sitemap->endSitemap();
	}	
}